<?php
class CWidget{
	public $w_wid=150;
	public $w_hei=190;
	public $id;
	public $content;
	public $domain;
	function __construct($widget_id){
		if (!empty($widget_id)){
			$this->id = $widget_id;
			$this->content = CTable('widget')->select($widget_id);
		}		
		$this->domain = 'http://'.$_SERVER['HTTP_HOST'];
	}
	function vie2eng($st)
	{
		$vietChar 	= 'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ|é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|ó|ò|ỏ|õ|ọ|ơ|ớ|ờ|ở|ỡ|ợ|ô|ố|ồ|ổ|ỗ|ộ|ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|í|ì|ỉ|ĩ|ị|ý|ỳ|ỷ|ỹ|ỵ|đ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ|Ó|Ò|Ỏ|Õ|Ọ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự|Í|Ì|Ỉ|Ĩ|Ị|Ý|Ỳ|Ỷ|Ỹ|Ỵ|Đ';
		$engChar	= 'a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|e|e|e|e|e|e|e|e|e|e|e|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|u|u|u|u|u|u|u|u|u|u|u|i|i|i|i|i|y|y|y|y|y|d|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|E|E|E|E|E|E|E|E|E|E|E|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|U|U|U|U|U|U|U|U|U|U|U|I|I|I|I|I|Y|Y|Y|Y|Y|D';
		$arrVietChar 	= explode("|", $vietChar);
		$arrEngChar		= explode("|", $engChar);
		return str_replace($arrVietChar, $arrEngChar, $st);
	}
	function create_image_trans($path){
		list($width, $height, $type, $attr) = getimagesize($path);
		$im = imagecreatetruecolor($width+5, $height);
		
		$while = imagecolorallocate($im, 255, 255, 255);
		$orange = imagecolorallocate($im, 255, 68, 0);
		
		// Make the background transparent
		imagecolortransparent($im, $while);
		
//		imagefilledrectangle($im, 0, 0, 4, 4, $orange);
		
		imagepng($im, $_SERVER['DOCUMENT_ROOT'].'/widgets/tran.png');
		imagedestroy($im);
	}
	function create_logo($wid,$skin,$size=""){
 		require_once $_SERVER['DOCUMENT_ROOT'].'/3rdparty/Asido/class.asido.php';
 		$path_dis = $_SERVER['DOCUMENT_ROOT'].'/widgets/images/templates/';
 		$path_font = $_SERVER['DOCUMENT_ROOT'].'/widgets/fonts/';
 		$path_temp = $_SERVER['DOCUMENT_ROOT'].'/widgets/templates/';
 		$path = CFile($path_dis)->mkdir();
 		$file_name = $path.$wid.'.jpg';
 		// logo info
 		$wName = 0;
 		$wWidth = 0;
 		$wCharSize = 0;
	switch ($size){
 			case 'medium':	$wWidth = 270;
 							$wName = 15;
 							$wCharSize =6;
 							$x=475;
 							$y=28;
 							break;
 			case 'large':	$wWidth = 382;
 							$wName = 20;
 							$wCharSize =9;
 							$x=675;
 							$y=28;
 							break;
 			default:
 				$wWidth = 270;
 				$wName = 15;
 				$wCharSize =6;
 				$x=475;
 				$y=28;
 		}
// 		$file_name1 = $path_dis.'near_complete/widget_'.$product['id'].'.jpg';
		$im = imagecreatefrompng($path_temp.$skin.'/'.$size.'logo.png');
		$widget = CTable('widget')->select($wid);
		$user = CTable('user')->select($widget['user_id']);
		$file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/widgets/templates'.'/'.$skin.'/settings.xml');
		$data_settings = CXml::xmlstring2array($file);
		// Create some colors
		//$data_settings['color-name'
		$cName = explode(',',$data_settings['color-name']);
		$cInfo = explode(',',$data_settings['color-info']);
		$color_name = imagecolorallocate($im, $cName[0], $cName[1], $cName[2]);
		$color_info = imagecolorallocate($im, $cInfo[0], $cInfo[1], $cInfo[2]);
		$black = imagecolorallocate($im, 0, 0, 0);
		session('choose_temp',$skin);
		
		// The text to draw
		// Replace path by your own font path
		$font = $path_font.'BoltonLight.ttf';
		$text = $this->vie2eng($widget['name']);
		
		$line1 = 75;
		$line2 =90;
		$line3 =105;
		$line1text='';
		$line2text='';
		$line3text='';
		if(empty($user['address'])){
			$line1text = $this->vie2eng($user['full_name']);
		}else{
			$line1text = $this->vie2eng($user['full_name'].' - '.$user['address']);
		}
		$line2text = 'Mobile: '.$user['phone'].'  Email:'.$user['email'];
		$line3text.=(empty($user['ym'])?'':'YM: '.$user['ym']);		
		$line3text.=(empty($user['skype'])?'':' Skype: '.$user['skype']);
		$avatar = '';
		if(!empty($user['avatar'])){
			$avatar = image($user['avatar']);
		}else{
			$avatar = 'portals/vimua/skins/default/images/no_avatar.jpg';
		}
//		$this->create_image_trans($avatar);	
		// Add the text
		$text = $this->vie2eng($text);
		imagestring($im, 3, ceil($wWidth-(strlen($line1text)*3.5)), $line1, $line1text , $color_info);
		imagestring($im, 3, ceil($wWidth-(strlen($line2text)*3.5)), $line2, $line2text , $color_info);
		imagestring($im, 3, ceil($wWidth-(strlen($line3text)*3.5)), $line3, $line3text , $color_info);
		imagettftext($im, $wName, 0, ceil($wWidth-(strlen($text)*$wCharSize)), 40, $color_name, $font, strtoupper($text));
		imagejpeg($im,$file_name);
		imagedestroy($im);

		asido::driver('gd');
		$i1 = asido::image($file_name, $file_name);
		Asido::watermark($i1, $avatar, array('x' => $x, 'y' => $y),ASIDO_WATERMARK_SCALABLE_ENABLED,0.4);
		$i1->save(ASIDO_OVERWRITE_ENABLED);	
 	}
 	/**
     * Lấy tên miền server chứa ảnh
     */
	function get_server_image(){
		//$data = portal()->data['settings']['ftp']['http-resource'];
	 return "http://www.vimua.com";
	}
	 /**
     * Lấy ảnh của sản phẩm trên server
     *
     * @param array $product thông tin của sản phẩm
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String địa chỉ ảnh sản phẩm
     */
	function get_product_image($product,$widget_id){
		//$product
		//$widget_id
		$product_image = $product['small'];
		if($product_image == '' && !empty($product['reference_id'])){
			$product1 = CTable('product')->select(array('code'=>$product['reference_id']));
			$product_image = $product1['small'];
		}
		if($product_image[0] != '/' && strpos($product_image,'http://') === false){
				$product_image = '/'.$product_image;
		}
 		return (strpos($product_image,'http://') === false?$this->get_server_image():'').$product_image;
 	}
 	/**
     * Hàm sử dụng tạo ảnh và liên kết của ảnh sản phẩm
     *
     * @param array $product thông tin của sản phẩm
     * @param int $commission hoa hồng của sản phẩm trong widget
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String liên kết của ảnh sản phẩm vừa tạo
     */
	function build_template_product_image($product,$commission,$widget_id){
		$product_image = $product['small'];
		if($product_image == '' && !empty($product['reference_id'])){
			$product1 = CTable('product')->select(array('code'=>$product['reference_id']));
			$product_image = $product1['small'];	
		}
		if($product_image[0] != '/' && strpos($product_image,'http://') === false){
				$product_image = '/'.$product_image;
		}
 		return '<a href="'.$this->domain.'/ban-hang/widget/'.$widget_id.'/'.$product['id'].'_'.(isset($product['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$product['name']):'').'.html" target="blank" style="text-decoration: none; color:#499AFF;"><img hspace="1"  src="'.(strpos($product_image,'http://') === false?$this->get_server_image():'').$product_image .'"></a>';
 	}
  	/**
     * Hàm sử dụng lấy tên và liên kết của tên sản phẩm
     *
     * @param array $product thông tin của sản phẩm
     * @param int $commission hoa hồng của sản phẩm trong widget
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String liên kết của tên sản phẩm vừa tạo
     */
	function build_template_product_name($product,$commission,$widget_id){
 		return '<a href="'.$this->domain.'/ban-hang/widget/'.$widget_id.'/'.$product['id'].'_'.(isset($product['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$product['name']):'').'.html" target="blank" style="text-decoration: none; color:#499AFF;padding:0;margin:0;" ><b>'.$product['name'].'</b></a>';
 	}
 	 /**
     * Lấy các thông tin về giá và nút mua ngay từ widget
     *
     * @param array $product thông tin của sản phẩm
     * @param int $commission hoa hồng của sản phẩm trong widget
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String liên kết của tên sản phẩm vừa tạo
     */
	function build_template_product($product,$commission,$widget_id){
	 		
	 		$content='<table class="widget" border="0" cellpadding="2" cellspacing="0" width="'.$this->w_wid.'px" >';
	 		$content.='<tr><td  align="center" style="font-size:9pt;color:#FF4400;"><img src="'.$this->domain.'/gian-hang/'.$widget_id.'-'.$product['id'].'/price-0.png?commission='.$commission.'&t='.time().'" style="padding:0;margin:0"></td></tr>';
	 		$content.='<tr><td align="center" style="font-size:9pt;">';
	 		$content .= '<a href="'.$this->domain.'/ban-hang/widget/'.$widget_id.'/'.$product['id'].'_'.(isset($product['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$product['name']):'').'.html" target="blank" style="text-decoration: none; color:#E06B01;"><img src="'.$this->domain.'/widgets/widget_buy_now.gif" alt="Mua ngay" title="Mua ngay" hspace="1" style="padding:0;margin:0"></a></td></tr>';
	 		$content.='</table>';
	 	return $content;
	 	}
	// begin widget forum
	// --------------------------------------------------------------------------
	 /**
     * Lấy các thông tin về ảnh sản phẩm cho widget forum
     *
     * @param array $product thông tin của sản phẩm
     * @param int $commission hoa hồng của sản phẩm trong widget
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String liên kết của ảnh sản phẩm vừa tạo
     */
	function build_template_product_image_forum($product,$commission,$widget_id,$link = 1){
		$product_image = $product['medium'];
		if($product_image == '' && !empty($product['reference_id'])){
			$product1 = CTable('product')->select(array('code'=>$product['reference_id']));
			$product_image = $product1['small'];	
		}
		if($product_image[0] != '/' && strpos($product_image,'http://') === false){
				$product_image = '/'.$product_image;
		}
		if($link){
			return '[*][url='.$this->domain.'/ban-hang/widget/'.$widget_id.'/'.$product['id'].'_'.(isset($product['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$product['name']):'').'.html]'.'[img]'.(strpos($product_image,'http://') === false?$this->get_server_image():'').$product_image .'[/img][/url]';
		}else{
			return '[*][img]'.(strpos($product_image,'http://') === false?$this->get_server_image():'').$product_image .'[/img]';
		}
 		
 	}
 	/**
     * Lấy tên ảnh sản phẩm cho widget forum
     *
     * @param array $product thông tin của sản phẩm
     * @param int $commission hoa hồng của sản phẩm trong widget
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String liên kết của tên sản phẩm vừa tạo
     */
	function build_template_product_name_forum($product,$commission,$widget_id,$link = 1){
		if($link){
			return '[*][URL='.$this->domain.'/ban-hang/widget/'.$widget_id.'/'.$product['id'].'_'.(isset($product['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$product['name']):'').'.html][B][size=150]'.$product['name'].'[/size][/B][/URL]';
		}else{
			return '[*][B]'.$product['name'].'[/B]';
		}
 		
 	}
 	/**
     * Lấy tên thông tin về giá và nút mua ngay cho forum
     *
     * @param array $product thông tin của sản phẩm
     * @param int $commission hoa hồng của sản phẩm trong widget
     * @param int $widget_id mã widget chứa sản phẩm
     * @return String html giá và nút mua ngay
     */
	function build_template_product_forum($product,$commission,$widget_id,$link = 1){
	 		
	 		$content='[*]';
	 		$content.='[color=red][size=150]Giá: [/size][/color][img]'.$this->domain.'/gian-hang/'.$widget_id.'-'.$product['id'].'/price-0.png?commission='.$commission.'&t='.time().'[/img]';
				if($link){
					$content .= '[*][URL='.$this->domain.'/ban-hang/widget/'.$widget_id.'/'.$product['id'].'_'.(isset($product['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$product['name']):'').'.html][IMG]'.$this->domain.'/widgets/widget_buy_now.gif[/IMG][/URL]';
				}else{
					$content .= '[*][IMG]'.$this->domain.'/widgets/widget_buy_now.gif[/IMG]';
				}
	 		
	 	return $content;
	 }
	// --------------------------------------------------------------------------
	// end widget forum
	
	 
		 /**
     * Lấy tên  món hời
     *
     * @return Lấy tên hàng món hời
     */
	 function get_name_product_special(){
	 	$condition1 = EAnd(EGT('quantity',0),EGT('price',0));
		$pmax  = CTable('product')->select($condition1,array('fields'=>'max(market_price - price) as vmax'));
	 	$condition = EAnd(EEqual('(market_price - price)',$pmax['vmax']),EGT('quantity',0),EGT('price',0));
		$product = CTable('product')->select($condition);
	 	return $product['name'];
	 }
	 /**
	 * Lấy thông tin của  món hời
     *
     * @return Lấy tên hàng món hời
     */
	 function get_product_special_info(){
	 	$condition1 = EAnd(EGT('quantity',0),EGT('price',0));
		$pmax  = CTable('product')->select($condition1,array('fields'=>'max(market_price - price) as vmax'));
	 	$condition = EAnd(EEqual('(market_price - price)',$pmax['vmax']),EGT('quantity',0),EGT('price',0));
		$product = CTable('product')->select($condition);
	 	return $product;
	 }
	/**
     * Tạo ra mã nhúng: flash,html,forum...
     *
     * @param Int  $widget_id là id của widget
     * @param Int $preview là trạng thái xem thử hay mã nhúng
     * @param Int $width chiều rộng widget
     * @param Int $height Chiều cao widget
     * @param Int $kw loại widget như: html,flash,forum...
     * @return String mã nhúng của widget tương ứng 
     */
    function get_content_widget($kw="html", $preview='0',$link=1, $monhoi=true, $logo=true) {
      //$widget_id
//      error_log($monhoi.'----'.$logo."\n<br>", 3, 'c:/delfolder.htm');
      $widget_id = $this->id;
      $pSpecial = $this->get_product_special_info();
      if(!$widget_id) exit;	
      $cols = $this->content['width'];
      $rows = $this->content['height'];
      $width = $this->w_wid * $cols;
      $height = $this->w_wid * $rows;
      if ($kw == "flash"){
      	// Xuất flash
      	return '<object width="'.$width.'" height="'.$height.'"><param name="movie" value="http://'.$_SERVER['HTTP_HOST'].'/widgets/productDisplay_'.$_SERVER['HTTP_HOST'].'.swf"/><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><PARAM NAME=FlashVars VALUE="widget_id='.$widget_id.'&preview='.$preview.'"><embed src="http://'.$_SERVER['HTTP_HOST'].'/widgets/productDisplay_'.$_SERVER['HTTP_HOST'].'.swf" type="application/x-shockwave-flash"  FlashVars="widget_id='.$widget_id.'"allowScriptAccess="always" allowFullScreen="true" width="'.$width.'" height="'.$height.'"></embed></object>';
      }else if($kw == "html"){
      	// Xuất html
      	$widget_content = CTable('widget')->select($widget_id);
      	eval('$products = '.$widget_content['products'].';');
      	$all_product = count($products);
      	$page = ceil($all_product/$cols);
      	//http://khoanb.vimua.com/ban-hang/346/902_Toshiba-Satellite-M300-S403--PSMDNL-008001-.html?special=yes
      	// begin header bar
      	$content = '<table border="0" class="widget" cellpadding="0" cellspacing="0" bgcolor="#ffffff">';
      		if ($logo) {
      		$content .= '<tr><td colspan="2" align="center">';
      		$content .= '<a href="#"><img src="'.$this->domain.'/gian-hang/logo.png?wid='.$widget_id.'&pid=12&t='.time().'" /></a>';
      		$content .= '</td></tr>';
      		}
      		 	// ads one
      		$content .= '<tr><td colspan="2" align="center">';
      		$content .= '<a href="#"><img src="'.$this->domain.'/gian-hang/separatorone.png?wid='.$widget_id.'&pid=12&t='.time().'" /></a>';
      		$content .= '</td></tr>';
      		if ($monhoi) {
      		$content .= '<tr><td colspan="2" align="center"><h1 style="color:red;"><b>MÓN HỜI HÔM NAY</b></h1><br><a target="blank" href="'.$this->domain.'/ban-hang/'.$widget_content['id'].'/'.$pSpecial['id'].'_'.(isset($pSpecial['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$pSpecial['name']):'').'.html?special=yes"><img src="'.$this->domain.'/gian-hang/specialimage.png?t='.time().'" /></a><br />';
      		$content .= '<a href="'.$this->domain.'/ban-hang/'.$widget_content['id'].'/'.$pSpecial['id'].'_'.(isset($pSpecial['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$pSpecial['name']):'').'.html?special=yes" target="blank"><img src="'.$this->domain.'/widgets/detail.png" /></a><br /></td></tr>';
      		}
      		 	// ads two
      		$content .= '<tr><td colspan="2" align="center">';
      		$content .= '<a href="#"><img src="'.$this->domain.'/gian-hang/separatortwo.png?wid='.$widget_id.'&pid=12&t='.time().'" /></a>';
      		$content .= '</td></tr>';
      		if ($logo) {
      		$content .= '<tr><td colspan="2" align="center">';
      		$content .= '<img src="'.$this->domain.'/gian-hang/term.png?temp='.(empty($widget_content['skin'])?'default':$widget_content['skin']).'&size='.(empty($widget_content['size'])?'':$widget_content['size']).'&t='.time().'" />';
      		$content .= '</td></tr>';
      		}
      		 	// ads three
      		$content .= '<tr><td colspan="2" align="center">';
      		$content .= '<a href="#"><img src="'.$this->domain.'/gian-hang/separatorthree.png?wid='.$widget_id.'&pid=12&t='.time().'" /></a>';
      		$content .= '</td></tr>';
      		
      		$content .= '<tr><td colspan="2" style="border:0px solid #84C4FF;" align="center">';
	      	$content .= '<table border="0" class="widget" cellpadding="0" cellspacing="2" width="100%" style="width:100%;">';
	      	$content .= '<tr><td colspan="'.$cols.'" style="height:5px;"></td></tr>';
	      	
      	// end header bar
      	
      	//product  image
      	$totalspecial = 0;
      	$totalnormal = 0;
	    foreach ($products as $product){
	    	if (empty($product['special'])){
	    		$totalnormal++;
	    	}else{
	    		$totalspecial++;
	    	}
	    }
      	for($i=0;$i<2;$i++)
      	{	
      		$count=0;
	      	$content_name='';
	      	$content_info='';
      		reset($products);
      		if($i == 0){
      			if($totalspecial > 0){
      				$content .= '<tr><td colspan="'.$cols.'" align="left" bgcolor="#90D4F8" height="35px" style="padding-left:5px;line-height:30px;"><span style="font-size:17px;font-weight:bold;color:#000;">Sản phẩm đặc biệt</span></td></tr>';
      			}
	      	}else{
	      		if($totalspecial > 0){
	      			$content .= '<tr><td colspan="'.$cols.'" align="left" bgcolor="#90D4F8" height="35px" style="padding-left:5px;line-height:30px;"><span style="font-size:17px;font-weight:bold;color:#000;">Sản phẩm khác</span></td></tr>';
	      		}
	      	}
	      	foreach ($products as $product){
	      		if($i == 0){
	      			if (empty($product['special'])) continue;
	      		}else{
	      			if (!empty($product['special'])) continue;
	      		}
	      		$count++;
	      		if($count%$cols == 1){
	      			$content .= '<tr>';
	      			$content_info .= '<tr>';
	      			$content_name .= '<tr>';
	      		}
	      	
	      		// in product
	      			$item = Ctable('product')->select($product['product_id']);
	      			$content .='<td align="center" style="border:0px solid #aaa;width:'.$this->w_wid.'px">'.$this->build_template_product_image($item, $product['commission'], $widget_id).'</td>';
	      			$content_name .= '<td align="center" valign="top" style="border:0px solid #aaa;line-height:20px;color:#499AFF;'.$this->w_wid.'px">'.$this->build_template_product_name($item,$product['commission'],$widget_id).'</td>';
	      			$content_info .= '<td  align="center" style="border:0px solid #aaa;width:'.$this->w_wid.'px">'.$this->build_template_product($item,$product['commission'],$widget_id).'</td>';
	      		$checkend = false;
	      		if($i == 0){
	      			if($count == $totalspecial){
	      				$checkend = true;
	      			}
	      		}else{
	      			if($count == $totalnormal){
	      				$checkend = true;
	      			}
	      		}
	      		if($count%$cols == 0 || $checkend){
	      			$content .= '</tr>';
	      			$content .= $content_name.'</tr>';
	      			$content .= $content_info.'</tr>';
	      			$content .= '<tr><td colspan="'.$cols.'" style="height:5px;"></td></tr>';
	      			$content_info = '';
	      			$content_name = '';
	      		}
	      		if($count == $all_product){
	      			$du = $page*$cols - $all_product;
	      			for($j=0;$j<$du;$j++){
	    				$content .= '<td style="border:0px solid #aaa;"></td>'; 
	    				$content_info .= '<td style="border:0px solid #aaa;"></td>';   				
	      			}
	      			$content .= '</tr>';
	      			$content .= $content_name.'</tr>';
	      			$content .= $content_info.'</tr>';
	      			$content_info = '';
	      			$content_name = '';
	      		}     			
	      	}
      	}
      	$content .= '</table></td></tr>';
      	 	// ads four
      		$content .= '<tr><td colspan="2" align="center">';
      		$content .= '<a href="#"><img src="'.$this->domain.'/gian-hang/separatorfour.png?wid='.$widget_id.'&pid=12&t='.time().'" /></a>';
      		$content .= '</td></tr>';
      	if ($logo) {	
      	$content .= '<tr><td colspan="2" align="center">';
      	$content .= '<img src="'.$this->domain.'/gian-hang/term.png?temp='.$widget_content['skin'].'&size='.(empty($widget_content['size'])?'':$widget_content['size']).'&t='.time().'" />';
      	$content .= '</td></tr>';
      	}
      	$content .= '<tr><td colspan="2" align="center">';
      	$content .= '<br />';
      	$content .= '<img src="'.$this->domain.'/gian-hang/widget-'.$widget_id.'/detect.png"';
      	$content .= '</td></tr>';
      	if ($logo) {
      	$content .= '<tr><td colspan="2" align="center">';
      	$content .= '<img src="'.$this->domain.'/gian-hang/logo.png?wid='.$widget_id.'&pid=12&t='.time().'" />';
      	$content .= '</td></tr>';
      	}
      	// ads five
      	$content .= '<tr><td colspan="2" align="center">';
      	$content .= '<a href="#"><img src="'.$this->domain.'/gian-hang/separatorfive.png?wid='.$widget_id.'&pid=12&t='.time().'" /></a>';
      	$content .= '</td></tr>';
      	$content .= '</table>';
      	return $content; 
      }else{
      	//forum
      	$widget_content = CTable('widget')->select($widget_id);
      	eval('$products = '.$widget_content['products'].';');
      	$all_product = count($products);
      	$page = ceil($all_product/$cols);
      	$content='';
      	// begin header bar
      	if ($logo) {
      		$content .= '[img]'.$this->domain.'/gian-hang/logo.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]';
      	}
      		// ads one
      		$content .= '[img]'.$this->domain.'/gian-hang/widget-'.$widget_id.'/detect.png'.'[/img]';
      		$content .= '[img]'.$this->domain.'/gian-hang/separatorone.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]';
      		if ($monhoi) {
      		$content .= '[list]'; 
      		$content .= '[*][size=150][color=red][b]MÓN HỜI HÔM NAY[/b][/color][/size]'; 
      		$content .= '[*][url='.$this->domain.'/ban-hang/'.$widget_content['id'].'/'.$pSpecial['id'].'_'.(isset($pSpecial['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$pSpecial['name']):'').'.html?special=yes][img]'.$this->domain.'/gian-hang/specialimage.png?time='.time().'[/img][/url]'; 
      		$content .= '[*][url='.$this->domain.'/ban-hang/'.$widget_content['id'].'/'.$pSpecial['id'].'_'.(isset($pSpecial['name'])?preg_replace('/[^a-zA-Z0-9_-]/','-',$pSpecial['name']):'').'.html?special=yes][img]'.$this->domain.'/widgets/detail.png[/img][/url]';
      		$content .= '[/list]';
      		}
      		// ads two
      		$content .= '[img]'.$this->domain.'/gian-hang/separatortwo.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]';
      	if ($logo) {
      		$content .= '[img]'.$this->domain.'/gian-hang/term.png?temp='.(empty($widget_content['skin'])?'default':$widget_content['skin']).'&size='.(empty($widget_content['size'])?'':$widget_content['size']).'&t='.time().'[/img]';
      	}
      		// ads three
      		$content .= '[img]'.$this->domain.'/gian-hang/separatorthree.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]'; 
      	// end header bar
        $totalspecial = 0;
      	$totalnormal = 0;
	    foreach ($products as $product){
	    	if (empty($product['special'])){
	    		$totalnormal++;
	    	}else{
	    		$totalspecial++;
	    	}
	    }
      	for($i=0;$i<2;$i++)
      	{
      		$content .= '[list=1]'; 
      		$count=0;
      		reset($products);
      		if($i == 0){
      			if($totalspecial > 0)
      				$content .= '[list][*][size=200][color=black]Sản phẩm đặc biệt[/color][/size][/list]';
	      	}else{
	      		if($totalspecial > 0)
	      		 $content .= '[list][*][size=200][color=black]Sản phẩm khác[/color][/size][/list]';
	      	}
	      	foreach ($products as $product){
	      		if($i == 0){
	      			if (empty($product['special'])) continue;
	      		}else{
	      			if (!empty($product['special'])) continue;
	      		}
	      		$count++;
	      		$item = Ctable('product')->select($product['product_id']);
	      		$content .= '[list]';
	      		$content .=$this->build_template_product_image_forum($item, $product['commission'], $widget_id,$link).'-';
	      		$content .=$this->build_template_product_name_forum($item,$product['commission'],$widget_id,$link).'-';
	      		$content .=$this->build_template_product_forum($item,$product['commission'],$widget_id,$link);
	      		$content .= '[/list]';
	      	}
	      	$content .='[/list]';
      	}
      	// ads four
      	$content .= '[img]'.$this->domain.'/gian-hang/separatorfour.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]';
      if ($logo) {
      	$content .= '[img]'.$this->domain.'/gian-hang/term.png?temp='.(empty($widget_content['skin'])?'default':$widget_content['skin']).'&size='.(empty($widget_content['size'])?'':$widget_content['size']).'&t='.time().'[/img]';
      }
      	// ads five
      	$content .= '[img]'.$this->domain.'/gian-hang/separatorfive.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]'; 
      if ($logo) {
      	$content .= '[img]'.$this->domain.'/gian-hang/logo.png?wid='.$widget_id.'&pid=12&t='.time().'[/img]';
      }      	
      	return $content; 
      }
    }
    
}
function CWidget($id){
	return new CWidget($id);
}
?>